#include "core.p4"
#include "v1model.p4"

// 最小版本 - 只包含基本功能
header source_routing_t {
    bit<8> path_len;
    bit<8> next_port;
}

header ethernet_t {
    bit<48> dstAddr;
    bit<48> srcAddr;
    bit<16> etherType;
}

struct metadata {
}

struct headers {
    ethernet_t ethernet;
    source_routing_t src_route;
}

parser MyParser(packet_in packet, out headers hdr, inout metadata meta) {
    state start {
        packet.extract(hdr.ethernet);
        transition select(hdr.ethernet.etherType) {
            0x1234: parse_src_route;
            default: accept;
        }
    }
    state parse_src_route {
        packet.extract(hdr.src_route);
        transition accept;
    }
}

control MyVerifyChecksum(inout headers hdr, inout metadata meta) { apply { } }

action drop_action() { mark_to_drop(standard_metadata); }

action src_route_forward(bit<9> port) { standard_metadata.egress_spec = port; }

table src_route_table {
    key = { hdr.src_route.isValid() : exact; }
    actions = { src_route_forward; drop_action; }
    default_action = drop_action;
}

control MyIngress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) {
    apply { src_route_table.apply(); }
}

control MyEgress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { apply { } }
control MyComputeChecksum(inout headers hdr, inout metadata meta) { apply { } }

control MyDeparser(packet_out packet, in headers hdr) {
    apply {
        packet.emit(hdr.ethernet);
        packet.emit(hdr.src_route);
    }
}

V1Switch(
    MyParser(),
    MyVerifyChecksum(),
    MyIngress(),
    MyEgress(),
    MyComputeChecksum(),
    MyDeparser()
) main;
